/*
 * @Description: 更多按钮
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2021-10-15 17:35:28
 * @LastEditTime: 2022-01-21 17:05:32
 * @LastEditTime: 2022-01-05 17:21:30
 * @FilePath: \cscec-pms-admin\src\components\MoreButton\index.js
 */
import store from '@/store'
export default {
  name: 'MoreButton',
  props: {
    isSpan: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      slotsList: (this.$slots?.default ?? []).filter(el => el.tag !== undefined),
      btnPermissionList: store.getters.btnPermission,
      timer: null,
      currentSlots: []
    }
  },
  created() {
    let slots = this.getSlotsList(this.slotsList)
    if(process.env?.VUE_APP_disabledBtnAuth === 'disabled') {
      this.currentSlots = slots
      return
    }
    const slotsArr = []
    slots.forEach(item => {
      if (this.findBtn(item)) {
        slotsArr.push(item)
      }
    })
    this.currentSlots = slotsArr
  },
  methods: {
    /**
     *
     * **/
    findBtn(node) {
      // 没有指令直接跳过
      if(!node.data || !node.data.directives || !node.data.directives.length) {
        return true
      }
      let idx = node.data.directives.findIndex(item => item.rawName === 'v-btnPermission' || item.rawName === 'v-btn-permission')
        if(idx == -1) {
          return true
        }

      let value = node.data.directives[idx].value
      if(!value) {
        return true
      }
      // console.log(value, 'chushihua')
      // to 需要判断数组
      const authCode = ['prma_seex', 'prma_buco', 'prma_buqu']
      const authPrefix = ['seex_', 'buco_', 'buqu_']
      const whiteCode = ['loo', 'moi', 'der', 'exp', 'pri', 'dow', 'dln','app','ann','exa']
      //    const activeCodes = store.state.user.activeCodes[1];//getQueryString('query')
      //     if (authCode.includes(activeCodes)) {
      // console.log(this.$store.state.user.activeCodes, 'this.$store.state.user.activeCodes')
      const activeCodes = this.$store.state.user.activeCodes[1]//this.$route.query.query
      if (authCode.includes(activeCodes)) {
        if (Array.isArray(value)) {
          //是数组 单独兼容
          // 兼容数组权限
          if (authCode.includes(activeCodes)) {
            // 判断是否是业务
            const idx = authCode.findIndex(item => item === activeCodes)
            if (idx == 2) {
              let flag = false
              for (let i = 0; i < whiteCode.length; i++) {
                const ele = whiteCode[i]
                if (value[0].code.indexOf(ele) > -1) {
                  flag = true
                  break
                }
              }
              return flag
            }
          }
          // console.log(value, '非业务')
          // 业务执行 业务管控
          for (let i = 0; i < value.length; i++) {
            const isIdent = activeCodes === value[i].ident ? true : false
            if (isIdent) {
              // console.log(value[i], '非业务')
              const index = this.btnPermissionList.findIndex(item => item.code === value[i].code)
              return index > -1 ? true : false
            }
          }
        } else {
          // console.log(value, 22222222222)
          // 非数组
          if (authCode.includes(activeCodes)) {
            // 业务3大tab菜单 需要加前缀
            const idx = authCode.findIndex(item => item === activeCodes)

            if (idx > -1 && idx !== 2) {
              value = authPrefix[idx] + value
              const isFind = this.btnPermissionList.findIndex((item) => item.code == value)
              return isFind > -1 ? true : false
            } else {
              // 匹配到业务查询了
              let flag = false
              for (let i = 0; i < whiteCode.length; i++) {
                const ele = whiteCode[i]
                if (value.indexOf(ele) > -1) {
                  flag = true
                  break
                }
              }
              // console.log(value, flag)
              return flag
            }
          }
        }

      } else {
        // console.log('非業務菜單', value)
        // 其他非业务菜单
        const isFind = this.btnPermissionList.findIndex((item) => item.code == value)
        return isFind > -1 ? true : false
      }
    },
    getSlotsList(slotsList) {

      const flatDom = (list) => {
        for (let i = 0; i < list.length; i++) {
          if (list[i].tag !== undefined) {
            if (list[i].componentOptions?.tag === 'a-divider') {
              list.splice(i, 1)
            } else {
              if (list[i]?.children) {
                flatDom(list[i].children)
              }
            }
          }
        }
      }
      flatDom(slotsList)
      return slotsList
    }
  },
  render(createElement, context) {
    let template = null
    if (this.isSpan) {
      let parent = this.$slots.default
      let slotsList = parent[0].children.filter(el => el.tag !== undefined)
      template = <div class={slotsList.length > 3 ? 'pub-table-list-action-auto' : 'pub-table-list-action'}>
        {slotsList.map((v, i, arr) => {
          if (arr.length > 3) {
            if (i < 2) {
              return v
            } else {
              if (i === 3) {
                return <a-dropdown>
                  <a className="ant-dropdown-link" onClick={e => e.preventDefault()}>更多
                    <a-icon type="down"/>
                  </a>
                  <a-menu slot="overlay">
                    {
                      arr.slice(2).map(el => {
                        return <a-menu-item style='color:#1890ff;'>{el}</a-menu-item>
                      })
                    }
                  </a-menu>
                </a-dropdown>
              }
            }
          } else {
            return v
          }
        })}
      </div>
    } else {
      let slotsList = this.currentSlots
      template = <div>
        {
          slotsList.length <= 2 ?
            <div>
              {
                slotsList.map((el, index) => {
                  if (index > 0) {
                    return <span><a-divider type="vertical"/><span>{el}</span></span>
                  } else {
                    return <span>{el}</span>
                  }
                }).filter((el, index) => index <= 2)
              }
            </div>
            : <div>
              {
                slotsList.map((el) => {
                  return <span>{el}</span>
                }).filter((el, index) => index === 0)
              }
              <a-divider type="vertical"/>
              <a-dropdown>
                <a
                  className="ant-dropdown-link"
                  onClick={e => e.preventDefault()}
                >
                  更多
                  <a-icon type="down"/>
                </a>
                <a-menu slot="overlay">
                  {
                    slotsList.map(el => {
                      return <a-menu-item>{el}</a-menu-item>
                    }).filter((el, index) => index > 0)
                  }
                </a-menu>
              </a-dropdown>
            </div>
        }
      </div>
    }
    return template
  }
}
